<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="128px">
            <el-form-item label="" prop="type">
                <el-select v-model="queryParams.type" placeholder="请选择工单类型" clearable>
                    <el-option v-for="dict in dict.type.repair_type" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="" prop="workNo">
                <el-input v-model="queryParams.workNo" placeholder="请输入工单编号" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="contacts">
                <el-input v-model="queryParams.contacts" placeholder="请输入联系人" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="contactNumber">
                <el-input v-model="queryParams.contactNumber" placeholder="请输入联系电话" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择工单状态" clearable>
                    <el-option v-for="dict in dict.type.install_status" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
             <el-form-item label="" prop="isDelay">
                <el-select v-model="queryParams.isDelay" placeholder="是否逾期" clearable>
                    <el-option v-for="dict in delayOptions" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['aftersales:repair:add']"
        >新增</el-button>
      </el-col> -->
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['aftersales:repair:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['aftersales:repair:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['aftersales:repair:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="repairList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <!-- <el-table-column label="ID" align="center" prop="id" /> -->
            <el-table-column label="订单编号" align="center" prop="orderNo" width="150" >
                <template slot-scope="scope">
                    <span v-if="scope.row.isDelay===1" style="color:red;">{{ scope.row.orderNo }}</span>
                    <span v-else>{{ scope.row.orderNo }}</span>
                </template>
                </el-table-column>
            <el-table-column label="联系人" align="center" prop="contacts" width="150" />
            <el-table-column label="联系电话" align="center" prop="contactNumber" width="150" />
            <el-table-column label="工单类型" align="center" prop="type">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.repair_type" :value="scope.row.type" />
                </template>
            </el-table-column>
            <el-table-column label="工单编号" align="center" prop="workNo" width="150" fixed="left" >
                <template slot-scope="scope">
                        <span v-if="scope.row.isDelay===1" style="color:red;">{{ scope.row.workNo }}
                            <el-tag size="mini" type="danger">
                            逾期
                        </el-tag></span>
                        <span v-else>{{ scope.row.workNo }}<el-tag size="mini">正常</el-tag></span>
                </template>
            </el-table-column>
            <el-table-column label="产品型号" align="center" prop="productModel" width="200" />
            <el-table-column label="地址" align="center" prop="address" width="200" />
            <el-table-column label="问题描述" align="center" prop="problem" width="300">
                <template slot-scope="scope">
                    <div v-html="scope.row.problem"></div>
                </template>
            </el-table-column>
            <el-table-column label="问题图片" align="center" prop="images" width="200">
                <template slot-scope="scope">
                    <image-preview :src="scope.row.images" :width="50" :height="50" />
                </template>
            </el-table-column>
            <el-table-column label="问题视频" align="center" prop="video" width="200">
                <template slot-scope="scope">
                    <video v-if="scope.row.video" :id="scope.row.id" :src="scope.row.video" controls width="160"
                        height="90" onclick="this.requestFullscreen()">
                    </video>
                    <span v-else></span>
                </template>
            </el-table-column>

            <el-table-column label="师傅信息" align="center" prop="workerId" width="180">
                <template slot-scope="scope">
                    <div>{{ scope.row.worker ? scope.row.worker.nickName : '' }}<br />
                        {{ scope.row.worker ? scope.row.worker.phonenumber : '' }}</div>
                </template>
            </el-table-column>
            <el-table-column label="上门日期" align="center" prop="conductTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.conductTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>

            <el-table-column label="工单完成时间" align="center" prop="finishTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="工单状态" align="center" prop="status" width="120" fixed="right">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.install_status" :value="scope.row.status" />
                </template>
            </el-table-column>
            <!-- <el-table-column label="评论状态" align="center" prop="commentStatus">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.comment_status" :value="scope.row.commentStatus" />
                </template>
            </el-table-column> -->
            <el-table-column label="备注" align="center" prop="remarks" width="200" />
            <el-table-column label="工单提交时间" align="center" prop="createTime" width="200" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120" fixed="right">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['aftersales:repair:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['aftersales:repair:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改售后维保对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="订单ID" prop="orderId">
                    <el-input v-model="form.orderId" placeholder="请输入订单ID" :disabled="true" />
                </el-form-item>
                <el-form-item label="工单编号" prop="workNo">
                    <el-input v-model="form.workNo" placeholder="请输入工单编号" :disabled="true" />
                </el-form-item>
                <el-form-item label="工单类型" prop="type">
                    <el-radio-group v-model="form.type">
                        <el-radio v-for="dict in dict.type.repair_type" :key="dict.value"
                            :label="parseInt(dict.value)">{{
                                dict.label }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="产品型号" prop="productModel">
                    <el-input v-model="form.productModel" placeholder="请输入产品型号" />
                </el-form-item>
                <el-form-item label="联系人" prop="contacts">
                    <el-input v-model="form.contacts" placeholder="请输入联系人" />
                </el-form-item>
                <el-form-item label="联系电话" prop="contactNumber">
                    <el-input v-model="form.contactNumber" placeholder="请输入联系电话" />
                </el-form-item>
                <el-form-item label="上门地址" prop="address">
                    <el-input v-model="form.address" placeholder="请输入地址" />
                </el-form-item>

                <el-form-item label="问题图片" prop="images">
                    <image-upload v-model="form.images" />
                </el-form-item>
                <el-form-item label="问题视频" prop="video">
                    <file-upload v-model="form.video" />
                </el-form-item>
                <el-form-item label="问题描述" prop="problem">
                    <el-input v-model="form.problem" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <el-form-item label="上门师傅" prop="salesmanId">
                    <el-select v-model="form.workerId" placeholder="请选择上门师傅" clearable :filterable="true">
                        <el-option v-for="item in userOptions" :key="item.userId" :label="item.nickName"
                            :value="item.userId">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="师傅上门日期" prop="conductTime">
                    <el-date-picker clearable v-model="form.conductTime" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择师傅上门日期">
                    </el-date-picker>
                </el-form-item>

                <el-form-item label="维修完成时间" prop="finishTime">
                    <el-date-picker clearable v-model="form.finishTime" type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择维修完成时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="工单状态" prop="status">
                    <el-radio-group v-model="form.status">
                        <el-radio v-for="dict in dict.type.install_status" :key="dict.value"
                            :label="parseInt(dict.value)">{{
                                dict.label }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <!-- <el-form-item label="评论状态" prop="commentStatus">
                    <el-radio-group v-model="form.commentStatus">
                        <el-radio v-for="dict in dict.type.comment_status" :key="dict.value"
                            :label="parseInt(dict.value)">{{
                                dict.label }}</el-radio>
                    </el-radio-group>
                </el-form-item> -->
                <el-form-item label="备注" prop="remarks">
                    <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
                </el-form-item>
                <!-- <el-form-item label="师傅OPENID" prop="workerId">
          <el-input v-model="form.workerId" placeholder="请输入师傅OPENID" />
        </el-form-item> -->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listRepair, getRepair, delRepair, addRepair, updateRepair } from "@/api/aftersales/repair";
import { listUser } from "@/api/system/user";
export default {
    name: "Repair",
    dicts: ['install_status', 'repair_type', 'comment_status'],
    data () {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 售后维保表格数据
            repairList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                type: null,
                workNo: null,
                contacts: null,
                contactNumber: null,
                status: null,
                commentStatus: null,
                isDelay: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                type: [
                    { required: true, message: "工单类型不能为空", trigger: "change" }
                ],
                address: [
                    { required: true, message: "地址不能为空", trigger: "blur" }
                ],
                problem: [
                    { required: true, message: "问题描述不能为空", trigger: "blur" }
                ],
                contacts: [
                    { required: true, message: "联系人不能为空", trigger: "blur" }
                ]
            },
            userOptions: [],
            delayOptions:[
                { label: '否',value:0 },
                { label: '是',value:1 }
            ]
        };
    },
    created () {
        this.getList();
        this.getUserTree();
    },
    methods: {
        getUserTree () {
            const params = { pageSize: 10, deptId: 204 };
            listUser(params).then(response => {
                this.userOptions = response.rows;
            });
        },
        /** 查询售后维保列表 */
        getList () {
            this.loading = true;
            listRepair(this.queryParams).then(response => {
                this.repairList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel () {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset () {
            this.form = {
                id: null,
                orderId: null,
                type: null,
                workNo: null,
                openId: null,
                productModel: null,
                address: null,
                problem: null,
                images: null,
                video: null,
                contacts: null,
                contactNumber: null,
                conductTime: null,
                remarks: null,
                finishTime: null,
                status: null,
                commentStatus: null,
                workerId: null,
                createTime: null,
                updateTime: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery () {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery () {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange (selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd () {
            this.reset();
            this.open = true;
            this.title = "添加售后维保";
        },
        /** 修改按钮操作 */
        handleUpdate (row) {
            this.reset();
            const id = row.id || this.ids
            getRepair(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改售后维保";
            });
        },
        /** 提交按钮 */
        submitForm () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateRepair(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addRepair(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete (row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除售后维保编号为"' + ids + '"的数据项？').then(function () {
                return delRepair(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport () {
            this.download('aftersales/repair/export', {
                ...this.queryParams
            }, `repair_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
